/**
 * (C) Copyright 2009, 2015 Pharmacodia Technology Co.,Ltd.
 */

package cn.qn.controller;

import cn.qn.dto.ResponseDTO;
import cn.qn.service.image.ImageService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Project ID：600YF17065
 * Resume：<br/>
 *
 * @author nishli
 * @see
 * @since 2017年2月22日
 */
@Controller
@RequestMapping(value = "oss")
public class OSSController {

    @Reference(check = false)
    private ImageService imageApi;

    @RequestMapping(value = "/getOSSPolicy")
    @ResponseBody
    public ResponseDTO getOSSPolicy(String filePath) {
        if (StringUtils.isBlank(filePath)) {
            filePath = "dev/app/default";
        } else {
            filePath = "dev/app/" + filePath;
        }

        return ResponseDTO.success(imageApi.generatePolicy("app", filePath));
    }

    @RequestMapping(value = "/getOSSProperties")
    @ResponseBody
    public ResponseDTO getOSSProperties(String filePath) {
        if (StringUtils.isBlank(filePath)) {
            filePath = "default";
        }
        return ResponseDTO.success(imageApi.getOSSProperties(filePath));
    }

    @RequestMapping(value = "/getOSSPrivatePolicy", method = RequestMethod.POST)
    @ResponseBody
    public ResponseDTO getOSSPrivatePolicy(String filePath) {
        return ResponseDTO.success(imageApi.generatePrivatePolicy(filePath));
    }

    @RequestMapping(value = "/getPresignedUrl", method = RequestMethod.POST)
    @ResponseBody
    public ResponseDTO generatePresignedUrl(String key) {
        return ResponseDTO.success(imageApi.generatePresignedUrl(key));
    }

}
